﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Super_Wall_Breaker.Sources.TeamElements;
using Super_Wall_Breaker.Sources.GameEngine;
using Super_Wall_Breaker.Sources.TeamElements.Players;
using Super_Wall_Breaker.Sources.TeamElements.PlayersControls;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Super_Wall_Breaker.Sources.GameElements.Balls;
using Super_Wall_Breaker.Sources.Configuration;

namespace Super_Wall_Breaker.Sources.Factories
{

    /// <summary>
    /// This class creates a Teams.
    /// </summary>
    public static class TeamsFactory
    {
        /// <summary>
        /// This function creates two default teams of one player. One real player versus an AI.
        /// </summary>
        /// <param name="balls"> The List of balls, used for the AI.</param>
        /// <returns> The list of the two teams created.</returns>
        public static List<Team> CreateDefaultTeams(List<Ball> balls, Vector2 speed)
        {
            List<Team> teams = new List<Team>();
            Team bottomTeam = new Team(TeamIndex.bottom);
            teams.Add(bottomTeam);
            Team topTeam = new Team(TeamIndex.top);
            teams.Add(topTeam);

            ControlsType controls = ControlsType.KEYBOARD;

#if XBOX
                controls = ControlsType.GAMEPAD;
#endif

            bottomTeam.AddPlayer(PlayerFactory.CreateAIPlayer(PlayerIndex.Three, balls, speed));
            topTeam.AddPlayer(PlayerFactory.CreateAIPlayer(PlayerIndex.Four, balls, speed));
            bottomTeam.AddPlayer(PlayerFactory.CreateRealPlayer(PlayerIndex.One, controls, speed));
            topTeam.AddPlayer(PlayerFactory.CreateRealPlayer(PlayerIndex.Two, controls, speed));

            return teams;
        }

        /// <summary>
        /// This function creates a list of team, according to the configuration set to the option menu.
        /// </summary>
        /// <param name="balls">The list of balls, used to the AI.</param>
        /// <param name="speed">The speed of the bats.</param>
        /// <returns>A list of team, created according to the configuration set to the option menu.</returns>
        public static List<Team> CreateConfiguredTeams(List<Ball> balls, Vector2 speed)
        {
            List<Team> teams = new List<Team>();
            Team bottomTeam = new Team(TeamIndex.bottom);
            teams.Add(bottomTeam);
            Team topTeam = new Team(TeamIndex.top);
            teams.Add(topTeam);

            GameCreationConfiguration gcc = GameCreationConfiguration.GetInstance();
            //The configuration of the game creation.

            // Player one creation.
            ControlsType playerOneControls = ControlsType.KEYBOARD;

            if(gcc.PlayerOneConfig.Equals(PlayerEntries.Gamepad))
                playerOneControls = ControlsType.GAMEPAD;

            bottomTeam.AddPlayer(PlayerFactory.CreateRealPlayer(PlayerIndex.One, playerOneControls, speed));

            // Player two creation.
            if(!gcc.PlayerTwoConfig.Equals(PlayerEntries.AI))
            {
                ControlsType playerTwoControls = ControlsType.KEYBOARD;

                if(gcc.PlayerTwoConfig.Equals(PlayerEntries.Gamepad))
                    playerTwoControls = ControlsType.GAMEPAD;

                topTeam.AddPlayer(PlayerFactory.CreateRealPlayer(PlayerIndex.Two, playerTwoControls, speed));
            }
            else
            {
                topTeam.AddPlayer(PlayerFactory.CreateAIPlayer(PlayerIndex.Two, balls, speed));
            }

            // Player three creation.
            if(!gcc.PlayerThreeConfig.Equals(PlayerEntries.None))
            {
                if(!gcc.PlayerThreeConfig.Equals(PlayerEntries.AI))
                {
                    ControlsType playerThreeControls = ControlsType.KEYBOARD;

                     if(gcc.PlayerThreeConfig.Equals(PlayerEntries.Gamepad))
                         playerThreeControls = ControlsType.GAMEPAD;

                    bottomTeam.AddPlayer(PlayerFactory.CreateRealPlayer(PlayerIndex.Three, playerThreeControls, speed));
                }
                else
                {
                    bottomTeam.AddPlayer(PlayerFactory.CreateAIPlayer(PlayerIndex.Three, balls, speed));
                }
            }

            // Player four creation.
            if(!gcc.PlayerFourConfig.Equals(PlayerEntries.None))
            {
                if(!gcc.PlayerFourConfig.Equals(PlayerEntries.AI))
                {
                    ControlsType playerFourControls = ControlsType.KEYBOARD;

                     if(gcc.PlayerFourConfig.Equals(PlayerEntries.Gamepad))
                         playerFourControls = ControlsType.GAMEPAD;

                    topTeam.AddPlayer(PlayerFactory.CreateRealPlayer(PlayerIndex.Four, playerFourControls, speed));
                }
                else
                {
                    topTeam.AddPlayer(PlayerFactory.CreateAIPlayer(PlayerIndex.Four, balls, speed));
                }
            }            

            return teams;
        }

        /// <summary>
        /// This function returns the color of a team, according to his index.
        /// </summary>
        /// <param name="teamIndex">The index of the team.</param>
        /// <returns>The color of the team.</returns>
        public static Color GetTeamColor(TeamIndex teamIndex)
        {
            if (teamIndex.Equals(TeamIndex.top))
            {
                return Color.LightCoral;
            }
            else
            {
                return Color.LightSeaGreen;
            }
        }
    }
}
